local zhuangshu = fk.CreateSkill{
    name = "ym1__zhuangshu"
}
Fk:loadTranslationTable{
    ["ym1__zhuangshu"] = "妆梳",
    [":ym1__zhuangshu"] = "游戏开始时或你失去一种颜色最后的手牌后，你可以将牌堆顶一张牌置入一名角色的宝物栏，其每回合首次失去时补全手牌花色。",
    ["#ym1__zhuangshu"] = "妆梳：你可以将牌堆顶一张牌置入一名角色的宝物栏",

    ["$ym1__zhuangshu1"] = "将军贵节志之人，妾亦当效仿。",
    ["$ym1__zhuangshu2"] = "我有千金宝镊，插之可增妩媚。",
}
zhuangshu:addEffect(fk.GameStart,{
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(zhuangshu.name)
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local targets = table.filter(room.alive_players, function (p)
            return table.contains(p:getAvailableEquipSlots(),"TreasureSlot")
        end)
        if #targets > 0 then
            local tos = room:askToChoosePlayers(player, {
                targets = targets,
                max_num = 1,
                min_num = 1,
                cancelable = true,
                skill_name = zhuangshu.name,
                prompt = "#ym1__zhuangshu"
            })
            if #tos > 0 then
                event:setCostData(self, tos[1])
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self)
        local cards = room:getNCards(1)
        local card = Fk:getCardById(cards[1])
        room:setCardMark(card, zhuangshu.name, 1)
        to:filterHandcards()
        --room:moveCardIntoEquip(to, card, zhuangshu.name, true, player)
        local moves = {}
        if not to:hasEmptyEquipSlot(Card.SubtypeTreasure) then
            local existingEquip = to:getEquipments(Card.SubtypeTreasure)
            p(existingEquip)
            table.insert(moves, {
                ids = to:getEquipments(Card.SubtypeTreasure),
                from = to,
                toArea = Card.DiscardPile,
                moveReason = fk.ReasonPutIntoDiscardPile,
                skillName = zhuangshu.name,
                proposer = player,
            })
        end
        table.insert(moves, {
            ids = { card.id },
            to = to,
            toArea = Card.PlayerEquip,
            moveReason = fk.ReasonPut,
            skillName = zhuangshu.name,
            proposer = player,
        })
        room:moveCards(table.unpack(moves))
    end,
})
zhuangshu:addEffect(fk.AfterCardsMove,{
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(zhuangshu.name) then
            for _, move in ipairs(data) do
                if move.from == player then
                    for _, info in ipairs(move.moveInfo) do
                        if
                            info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId).color ~= Card.NoColor and
                            not table.find(player:getCardIds("h"), function (id)
                                return Fk:getCardById(id).color == Fk:getCardById(info.cardId).color
                            end)
                        then
                            return true
                        end
                    end
                end
            end
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local targets = table.filter(room.alive_players, function (p)
            return table.contains(p:getAvailableEquipSlots(),"TreasureSlot")
        end)
        if #targets > 0 then
            local tos = room:askToChoosePlayers(player, {
                targets = targets,
                max_num = 1,
                min_num = 1,
                cancelable = true,
                skill_name = zhuangshu.name,
                prompt = "#ym1__zhuangshu"
            })
            if #tos > 0 then
                event:setCostData(self, tos[1])
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self)
        local cards = room:getNCards(1)
        local card = Fk:getCardById(cards[1])
        room:setCardMark(card, zhuangshu.name, 1)
        to:filterHandcards()
        --room:moveCardIntoEquip(to, card, zhuangshu.name, true, player)
        local moves = {}
        if not to:hasEmptyEquipSlot(Card.SubtypeTreasure) then
            local existingEquip = to:getEquipments(Card.SubtypeTreasure)
            table.insert(moves, {
                ids = {existingEquip[1]},
                from = to,
                toArea = Card.DiscardPile,
                moveReason = fk.ReasonPutIntoDiscardPile,
                skillName = zhuangshu.name,
                proposer = player,
            })
        end
        table.insert(moves, {
            ids = { card.id },
            to = to,
            toArea = Card.PlayerEquip,
            moveReason = fk.ReasonPut,
            skillName = zhuangshu.name,
            proposer = player,
        })
        room:moveCards(table.unpack(moves))
    end,
})
zhuangshu:addEffect("filter", {
    global = true,
    card_filter = function(self, to_select, player, isJudgeEvent)
        return to_select:getMark(zhuangshu.name) ~= 0
    end,
    view_as = function(self, player, to_select)
        return Fk:cloneCard("treasure__ym__zhuangshu", to_select.suit, to_select.number)
    end,
})
zhuangshu:addEffect(fk.BeforeCardsMove, {
    can_refresh = function(self, event, target, player, data)
        return
        not (data.extra_data or {}).zhuangshuIds and
        table.find(data, function(move)
            return table.find(move.moveInfo, function(info)
                return Fk:getCardById(info.cardId):getMark(zhuangshu.name) ~= 0
            end) and move.from
        end)
    end,
    on_refresh = function(self, event, target, player, data)
        local zhuangshuIds = {}
        for _, move in ipairs(data) do
            for _, info in ipairs(move.moveInfo) do
                if Fk:getCardById(info.cardId):getMark(zhuangshu.name) > 0 and move.from and move.from:getMark("zhuangshu_lose-turn") == 0 then
                    player.room:setPlayerMark(move.from,"zhuangshu_lose-turn",1)
                    table.insert(zhuangshuIds, {move.from, info.cardId})
                end
            end
        end

        data.extra_data = data.extra_data or {}
        data.extra_data.zhuangshuIds = zhuangshuIds
    end,
})
zhuangshu:addEffect(fk.AfterCardsMove, {
    mute = true,
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(zhuangshu.name) and type((data.extra_data or {}).zhuangshuIds) == "table"
    end,
    on_cost = Util.TrueFunc,
    on_use = function (self, event, target, player, data)
        local room = player.room
        for _, dat in ipairs(data.extra_data.zhuangshuIds) do
            local to = dat[1]
            local suits = {Card.Spade,Card.Club,Card.Heart,Card.Diamond}
            for _, id in ipairs(to:getCardIds("h")) do
                table.removeOne(suits, Fk:getCardById(id).suit)
            end
            local get = {}
            for _, id in ipairs(room.draw_pile) do
                if #suits > 0 then
                local suit = Fk:getCardById(id).suit
                    if table.contains(suits, suit) then
                        table.insert(get, id)
                        table.removeOne(suits, suit)
                    end
                end
            end
            if #get > 0 then
                room:obtainCard(to, get, false, fk.ReasonPrey)
            end
        end
    end,
})
return zhuangshu